#!/bin/sh

ret=0
flags="${1//[!zPpEeMmRrSs]}"
[ -n "${flags}" ] || exit 0
shift

# Create XATTR_PAX marking using attr
xval=""
[ "${flags//[!P]}" ] && xval="${xval}P"
[ "${flags//[!p]}" -a -z "${flags//[!P]}" ] && xval="${xval}p"
[ "${flags//[!E]}" ] && xval="${xval}E"
[ "${flags//[!e]}" -a -z "${flags//[!E]}" ] && xval="${xval}e"
[ -z "${flags//[!zEe]}" ] && xval="${xval}e"
[ "${flags//[!M]}" ] && xval="${xval}M"
[ "${flags//[!m]}" -a -z "${flags//[!M]}" ] && xval="${xval}m"
[ "${flags//[!R]}" ] && xval="${xval}R"
[ "${flags//[!r]}" -a -z "${flags//[!R]}" ] && xval="${xval}r"
[ "${flags//[!S]}" ] && xval="${xval}S"
[ "${flags//[!s]}" -a -z "${flags//[!S]}" ] && xval="${xval}s"

for f in "$@"; do
	if [ -n "FAKEROOTKEY" ]; then
		# fakeroot does not set xattr's on disk.
		# explicitly do that, because the marked binary might
		# be executed next during install. But don't fail on it
		# because if xattr is not supported it's not hardened kernel.
		LD_PRELOAD="" FAKEROOTKEY="" attr -q -s pax.flags -V "${xval}" "${f}" &>/dev/null
	fi
	attr -q -s pax.flags -V "${xval}" "${f}" >/dev/null || ret=1
done

exit $ret

